﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum.工具类
{
    class LoadListView
    {
        public static void LoadQueue(string DML)
        {
            //----正在下载和等待下载

            Loads.client.QueueView.Items.Clear();//清除所有项,避免重复！
            
            SqlDataReader reader = DBHelps.Select(DML);

            while (reader.Read())
            {
                //创建ListViewItem对象，并设置Text属性
                ListViewItem item1 = new ListViewItem(reader["文件名"].ToString());

                //向ListViewItem对象中依次添加子项
                item1.SubItems.Add(reader["文件大小"].ToString());
                item1.SubItems.Add(reader["状态"].ToString());

                //向ListView控件中添加项
                Loads.client.QueueView.Items.Add(item1);

                //设置该文件图标
                string Suffix = (reader["文件类型"].ToString());
                item1.ImageIndex = IfSuffix.IfSuffixs(Suffix);

                //item1.SubItems.
            }
            reader.Close();
            DBHelps.reader = null;
        }
        public static void LoadMyFlie(string SelectDML)
        {

        }



        public static void CountQueue()//统计列表的方法
        {
            int downloadcount = 0;//下载计数
            int uploadcount = 0;//上传计数
            int endcount=0;//传输完成计数

            //1.统计完成数
            string SelectEnd = "select SaveName 文件名,OperationState 状态,cast((FileSize/1024/1024)as decimal(20,3)) 文件大小,FileSuffix 文件类型 from FlieTeamLog where OperationState= '下载完成' or OperationState = '上传完成'";
            SqlDataReader reader1 = DBHelps.Select(SelectEnd);
            while (reader1.Read())
            {
                endcount++;
            }
            reader1.Close();
            DBHelps.reader = null;

            //System.InvalidOperationException:“线程间操作无效: 从不是创建控件“CurrentlyEnd”的线程访问它。”

            //lock (this)
            //{
            //
            //}
            //公共变量需要加设备锁
            Loads.client.CurrentlyEnd.Text = "传输完成（" + endcount + ")";

            //2.统计下载数
            string SelectDownload = "select SaveName 文件名,OperationState 状态,cast((FileSize/1024/1024)as decimal(20,3)) 文件大小,FileSuffix 文件类型 from FlieTeamLog where OperationState= '等待下载' or OperationState = '正在下载'";
            SqlDataReader reader2 = DBHelps.Select(SelectDownload);
            while (reader2.Read())
            {
                downloadcount++;
            }
            reader2.Close();
            DBHelps.reader = null;

            //System.InvalidOperationException:“线程间操作无效: 从不是创建控件“CurrentlyEnd”的线程访问它。”
            Loads.client.CurrentlyDownload.Text = "正在下载（" + downloadcount + ")";

            //3.统计上传数
            string SelectUpload = "select SaveName 文件名,OperationState 状态,cast((FileSize/1024/1024)as decimal(20,3)) 文件大小,FileSuffix 文件类型 from FlieTeamLog where OperationState= '等待上传' or OperationState = '正在上传'";
            SqlDataReader reader3 = DBHelps.Select(SelectUpload);
            while (reader3.Read())
            {
                uploadcount++;
            }
            reader3.Close();
            DBHelps.reader = null;

            Loads.client.CurrentlyUpload.Text = "正在上传（" + uploadcount + ")";


            try
            {
                switch (Client.RefreshListindex)
                {
                    case 0:
                        LoadListView.LoadQueue("select SaveName 文件名,OperationState 状态,cast((FileSize/1024/1024)as decimal(20,3)) 文件大小,FileSuffix 文件类型 from FlieTeamLog where OperationState= '等待下载' or OperationState = '正在下载'");
                        break;
                    case 1:
                        LoadListView.LoadQueue("select SaveName 文件名,OperationState 状态,cast((FileSize/1024/1024)as decimal(20,3)) 文件大小,FileSuffix 文件类型 from FlieTeamLog where OperationState= '等待上传' or OperationState = '正在上传'");
                        break;
                    case 2:
                        LoadListView.LoadQueue("select SaveName 文件名,OperationState 状态,cast((FileSize/1024/1024)as decimal(20,3)) 文件大小,FileSuffix 文件类型 from FlieTeamLog where OperationState= '下载完成' or OperationState = '上传完成'");
                        break;
                    default:
                        MessageBox.Show("刷新无效");
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("刷新队列错误");
                throw;
            }





        }
    }
}
